#include "report_handle.h"
static TaskHandle_t thread_report;
iot_dazzling_info_t status_dazzling_info;

static u8 buffer[SIZE];
static u8 buffer_send[SIZE];
static u16 send_flag=0;

void report_dazzling_handle()
{
    if (0 != send_flag)
    {
        int i=0;
        buffer_send[0]= 0xA5;
        i +=1;
        //DI
        memcpy(&buffer_send[1+2],buf_device,16);
        send_flag+=16;
        i+=16;
        //MI
        u32 mess;
        mess = get_message();
        memcpy(&buffer_send[1+16+2],&mess,4);
        send_flag +=4;
        i +=4;
        memcpy(&buffer_send[1],&send_flag,sizeof(u16));
        i+= sizeof(u16);
        memcpy(&buffer_send[i],buffer,(send_flag-4-16));
        i+=(send_flag-4-16);
        buffer_send[i] =0;
        i +=1;
        buffer_send[i] =0;
        i +=1;
        buffer_send[i] =0x5A;
        i +=1;
        TCP_send_data(buffer_send,i);
        memset(buffer,0,send_flag);
        memset(buffer_send,0,i);
        i=0;
        send_flag = 0;
    }
}

void iot_report_init()
{
    memset(&status_dazzling_info,0,sizeof(iot_dazzling_info_t));
}

void set_main_mode_dazzling_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_flag],&code,sizeof(u32));
    send_flag +=4;
    buffer[send_flag] = num;
    send_flag +=1;
}

void set_RGB_brighness_dazzling_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_flag],&code,sizeof(u32));
    send_flag +=4;
    buffer[send_flag] = num;
    send_flag +=1;
}

void set_custom_part_dazzling_handle(u32 code,PART_INFO_T part_info)
{
    memcpy(&buffer[send_flag],&code,sizeof(u32));
    send_flag +=4;
    buffer[send_flag] = part_info.len; 
    send_flag +=1;
    memcpy(&buffer[send_flag],part_info.RGB,45);
    send_flag +=45;
}

void set_custom_wait_dazzling_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_flag],&code,sizeof(u32));
    send_flag +=4;
    buffer[send_flag] = num;
    send_flag +=1;
}

void set_scene_mode_dazzling_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_flag],&code,sizeof(u32));
    send_flag +=4;
    buffer[send_flag] = num;
    send_flag +=1;
}

void set_light_effect_ssp_dazzling_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_flag],&code,sizeof(u32));
    send_flag +=4;
    buffer[send_flag] = num;
    send_flag +=1;
}

void set_custom_ssp_dazzling_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_flag],&code,sizeof(u32));
    send_flag +=4;
    buffer[send_flag] = num;
    send_flag +=1;
}

void iot_report_handle()
{
    static int time;
    /*********************主模式相关设置******************************************************/
    if (status_dazzling_info.save_ssp != app_logic_get_ssp())
    {
        status_dazzling_info.save_ssp= app_logic_get_ssp();
        set_main_mode_dazzling_handle(MAIN_MODE,status_dazzling_info.save_ssp);
    }
    /*********************RGB灯珠相关设置******************************************************/
    if (status_dazzling_info.brighness_level != get_brighness_level())
    {
        status_dazzling_info.brighness_level = get_brighness_level();
        set_RGB_brighness_dazzling_handle(RGB_BRIGHNESS,status_dazzling_info.brighness_level);
    }

    /*********************场景相关相关设置******************************************************/
    if(status_dazzling_info.scene_mode != get_scene_ssp())
    {
        status_dazzling_info.scene_mode = get_scene_ssp();
        set_scene_mode_dazzling_handle(SCENE_MODE,status_dazzling_info.scene_mode);
    }

    /*********************自定义相关相关设置******************************************************/
    if((status_dazzling_info.custom_part.len != light_effect_config.len)||memcmp(status_dazzling_info.custom_part.RGB,light_effect_config.buf,light_effect_config.len))
    {
        status_dazzling_info.custom_part.len = light_effect_config.len;
        memcpy(status_dazzling_info.custom_part.RGB,light_effect_config.buf,light_effect_config.len);
        set_custom_part_dazzling_handle(CUSTOM_PART_DATA,status_dazzling_info.custom_part);
    }

    if(status_dazzling_info.wait != get_light_wait())
    {
        status_dazzling_info.wait = get_light_wait();
        set_custom_wait_dazzling_handle(CUSTOM_WAIT,status_dazzling_info.wait);
    }

    if(status_dazzling_info.light_effect_ssp != get_light_effect_enum())
    {
        status_dazzling_info.light_effect_ssp = get_light_effect_enum();
        set_light_effect_ssp_dazzling_handle(EFFECT_SSP,status_dazzling_info.light_effect_ssp);
    }

    if(status_dazzling_info.custom_ssp != get_custom_ssp())
    {
        status_dazzling_info.custom_ssp = get_custom_ssp();
        set_custom_ssp_dazzling_handle(CUSTOM_SSP,status_dazzling_info.custom_ssp);
    }

    report_dazzling_handle();

    if(arch_os_ms_elapsed(time)>=(30*1000))//每30s上报一次心跳
    {
        heart_beat_handle();
        time=arch_os_ms_now();
    }
}

void iot_report_loop(void *pvParameters)
{
    LOG_INFO("INTO iot_report_loop\n");
    iot_report_init();
    while (1)
    {
        if(get_tcp_status())
        {
            iot_report_handle();
        }
        vTaskDelay(pdMS_TO_TICKS(IOT_REPORTE_SPEED_MS)); //延时20ms
    }
}

void task_iot_report_init()
{
    xTaskCreate(iot_report_loop,"iot_report_dazzling_loop",3072,NULL,TASK_PROP_IOTTALK,&thread_report);
}